package com.fingard.dsp.tcpserver;

import com.fingard.FGBiz;
import com.fingard.FgVersion;
import com.fingard.constant.Format;
import com.fingard.crypto.SignEncryptHelper;
import com.fingard.diagnostics.LogHelper;
import com.fingard.dsp.DSPCenter;
import com.fingard.dsp.Transmit;
import com.fingard.dsp.bank.BizBankObject;
import com.fingard.dsp.bank.DealDirectBank;
import com.fingard.dsp.bank.dbobj.secretkey.SecretKeyDataHelper;
import com.fingard.dsp.bank.reqrespobj.ReqRespHeader;
import com.fingard.text.StringHelper;
import com.fingard.text.XmlHelper;
import com.fingard.util.AESUtil;
import com.fingard.util.DateHelper;
import com.fingard.util.secret.salary.FieldHiddenUtil;
import com.fingard.xml.XmlTextReader;
import org.apache.commons.codec.binary.Base64;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class RequestNIO extends BizBankObject{
	public HttpServletRequest request;
	public CyclicBarrier barrier;
	public String xmlData;
	public String respData = "";

	public RequestNIO(String xmlData, HttpServletRequest request,CyclicBarrier barrier) {
		super();
		this.request = request;
		this.xmlData = xmlData;
		this.barrier = barrier;
	}

	public boolean isRemotingInvoke = false;
	
	public boolean isInvokeByHttp = false;
	
	public boolean isHttpDistribute = false;
	
	public Transmit transmit = null;
	
    public ByteBuffer recByteBuffer;
    
    public RequestNIO(SocketChannel p_socketChannel, String p_charsetName) {
    	super();
    	reqSocketReceiver = new SocketReceiver(p_socketChannel, p_charsetName);
    	recByteBuffer = null;
	}
    
    public RequestNIO(Socket p_socket, String p_charsetName) {
    	super();
    	reqSocketReceiver = new SocketReceiver(p_socket, p_charsetName);
    	recByteBuffer = null;
	}
    
    public RequestNIO(SocketReceiver pSocketReceiver) {
    	super();
    	reqSocketReceiver = pSocketReceiver;
    	recByteBuffer = null;
	}
    
    /**是否已经*/
    public boolean isAlreadyWriteResp = false;
    /**将报文写入请求的返回字节流
     * @throws IOException */
    public void writeRespBody(boolean pIsRtnFileBytes, String pRtnFilePath, String pRespBody) throws IOException{
    	if(isAlreadyWriteResp){//已经返回报文了
    		return;
    	}
    	boolean isRtnFileBytes = pIsRtnFileBytes;
    	String tmpRespBody = pRespBody;
    	byte[] tmpBytesResp = null;
        if(isRtnFileBytes){
        	isRtnFileBytes = false;
        	File tmpFile = new File(pRtnFilePath);
        	Long filelength = tmpFile.length();
        	int filelenInt = filelength.intValue();
        	String tmpFileLen = String.format("% 8d", filelenInt);
            String headStr = "file-length:" + tmpFileLen + "\r\n\r\n";
            byte[] headBytes = headStr.getBytes(reqSocketReceiver.charsetName);

            tmpBytesResp = new byte[headBytes.length + filelenInt];
            System.arraycopy(headBytes, 0, tmpBytesResp, 0, headBytes.length);
            FileInputStream in = null;
            try {  
                in = new FileInputStream(tmpFile);
                in.read(tmpBytesResp, headBytes.length, filelenInt);
                in.close();
                in = null;
            } catch (FileNotFoundException e) {
            	isRtnFileBytes = false;
            	tmpRespBody = LogHelper.getStackTrace(e);
            	sbLog.append(LogHelper.getStackTrace(e));
            } catch (IOException e) {
            	isRtnFileBytes = false;
            	tmpRespBody = LogHelper.getStackTrace(e);
            	sbLog.append(LogHelper.getStackTrace(e));
            }finally{
            	if(in != null){
            		in.close();
            	}
            }
        }
        if(!isRtnFileBytes){
        	if(tmpRespBody.startsWith("java") || !tmpRespBody.contains("<")){
            	tmpRespBody = "<ATSYH><TransResp><TransParam><RespCode>-1</RespCode><RespInfo>"+XmlHelper.encode(tmpRespBody)+"</RespInfo></TransParam></TransResp></ATSYH>";
            }
            if (reqSocketReceiver.readedLength == 0 && request == null) {
                //F5等设备的监测
            	reqSocketReceiver.clientSocketChannel.close();
            	reqSocketReceiver.clientSocketChannel = null;
                return;
            }
            byte[] tmpBytesBody = tmpRespBody.getBytes(reqSocketReceiver.charsetName);

            String tmpStrLen = String.format("% 8d", tmpBytesBody.length);

            StringBuilder tmpRespHead = new StringBuilder();
            if(reqSocketReceiver.isHttp || request != null){
            	if(FGBiz.dspCenter.dspSet.enableHttp){
            		tmpRespHead.append("HTTP/1.1 200 OK\r\n");
                	tmpRespHead.append("Server: DSP Web Server\r\nDate: "+Format.toGMT(new Date())+"\r\n");
    				tmpRespHead.append("Content-Type: text/xml; charset="+reqSocketReceiver.charsetName+"\r\n");
    				//tmpRespHead.append("Content-Length: " + tmpStrLen + "\r\nConnection: close\r\n\r\n");
    				tmpRespHead.append("Content-Length: " + tmpStrLen + "\r\n\r\n");
    				sbLog.append("响应http头部：\r\n"+tmpRespHead.toString()+"\r\n");
            	}else{
            		FGBiz.limitMsg.error("RequestNIO.writeRespBody", "未开启http识别");
            		//未开启http识别，直接关闭连接返回
            		reqSocketReceiver.clientSocketChannel.close();
            		reqSocketReceiver.clientSocketChannel = null;
            		isAlreadyWriteResp = true;
                    return;
            	}
            }else{
            	tmpRespHead.append("Content-Length:" + tmpStrLen + "\r\n");
            }
            byte[] tmpBytesHead = tmpRespHead.toString().getBytes(reqSocketReceiver.charsetName);

            tmpBytesResp = new byte[tmpBytesHead.length + tmpBytesBody.length];
            System.arraycopy(tmpBytesHead, 0, tmpBytesResp, 0, tmpBytesHead.length);
            System.arraycopy(tmpBytesBody, 0, tmpBytesResp, tmpBytesHead.length, tmpBytesBody.length);
        }
        if(isRemotingInvoke){
        	writeBytesForRemoting(tmpBytesResp);
        	isAlreadyWriteResp = true;
        }else if(request != null && StringHelper.hasAnyChar(xmlData)){
//			response.getOutputStream().write(tmpBytesResp);
			respData = new String(tmpBytesResp,reqSocketReceiver.charsetName);
//			System.out.println("处理完成"+new Date());
			try {
				barrier.await();//阻塞
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (BrokenBarrierException e) {
				e.printStackTrace();
			}
		}else if(isInvokeByHttp){
        	DataOutputStream tmpRespOutput = new DataOutputStream(reqSocketReceiver.socketClient.getOutputStream());
			tmpRespOutput.write(tmpBytesResp);
			tmpRespOutput.close();
			reqSocketReceiver.socketClient.close();
        }else{
        	ByteBuffer writeBuffer = ByteBuffer.wrap(tmpBytesResp);
        	reqSocketReceiver.clientSocketChannel.configureBlocking(true);
        	reqSocketReceiver.clientSocketChannel.write(writeBuffer);
        	isAlreadyWriteResp = true;
        	reqSocketReceiver.clientSocketChannel.close();
        	reqSocketReceiver.clientSocketChannel = null;
        }
    }
    
    public ReqRespHeader bankHeader = new ReqRespHeader();
    public XmlTextReader xmlReader = null;
    public StringBuilder sbReq = null;
    public String clientReqStr = "";
    
    public void GetReqRespHeader() throws Exception{
    	//if(reqSocketReceiver.reqBytes.length>=reqSocketReceiver.readedLength){
    		//String oriStr = new String(reqSocketReceiver.reqBytes, 0, reqSocketReceiver.readedLength, reqSocketReceiver.charsetName);
        	//FGBiz.limitMsg.addConsole("RequestNIO.ori", oriStr);
    	//}
    	if(!reqSocketReceiver.isSyncFile && !"getfile".equals(reqSocketReceiver.action)){
    		if(reqSocketReceiver.contentStartIndex >= 0){
    			clientReqStr = new String(reqSocketReceiver.reqBytes, reqSocketReceiver.contentStartIndex, reqSocketReceiver.contentLength, reqSocketReceiver.charsetName);
    		}else if(request != null && StringHelper.hasAnyChar(xmlData)){
				clientReqStr = xmlData;
			} else{
    			reqSocketReceiver.contentStartIndex = 0;
    			if(reqSocketReceiver.contentLength<=0){
    				reqSocketReceiver.contentLength = reqSocketReceiver.readedLength;
    			}
    			clientReqStr = new String(reqSocketReceiver.reqBytes, 0, reqSocketReceiver.contentLength, reqSocketReceiver.charsetName);
    		}
    		sbReq = new StringBuilder();
    		sbReq.append(clientReqStr);
    		if(!"savetodb".equals(reqSocketReceiver.action)){
    		    if (!clientReqStr.startsWith("Action:sssmap")) {
                    sbLog.append("系统版本号:" + FgVersion.DspVersion + "\r\n");
                    sbLog.append(Format.DateTimeSSSFormat.format(connTime) + " " + seqIDInner + " 0.开始接收，来源IP："+reqSocketReceiver.getClientIp()+"\r\n");
                    sbLog.append(Format.DateTimeSSSFormat.format(recCompleteTime) + " 1.接收ATS的报文：\r\n");
                    //如果ats传私钥过过来需要进行脱密
                    if(clientReqStr.contains("<DecryptKey>")){
                        sbLog.append(FieldHiddenUtil.wordHiddle(clientReqStr,new String[]{"DecryptKey"}));
                    }else{
                        sbLog.append(clientReqStr);
                    }

                    //如果数据是加密或者签名的，需要解密和验签
                    dataDecryptAndChk();

                    sbLog.append("\r\n\r\n");
                }
                if(clientReqStr.contains("<") && clientReqStr.contains(">")) {
                    try {
                        xmlReader = new XmlTextReader(sbReq);
                        bankHeader = DSPCenter.getReqRespHeader(xmlReader);
                    } catch (Exception ex) {
                        FGBiz.limitMsg.exception("RequestNIO.GetReqRespHeader", ex, sbReq.toString());
                    }
                } else if (clientReqStr.startsWith("Action:sssmap")) {
					StringReader strReader = new StringReader(clientReqStr);
					BufferedReader tmpReader = new BufferedReader(strReader);

					HashMap<String, String> hashParams = new HashMap<String, String>();
					while (true) {
						String eachline = tmpReader.readLine();
						if (eachline == null) {
							break;
						}

						String separator = "";
						if (eachline.indexOf("=") > 0 && eachline.indexOf(":") > 0) {
							if (eachline.indexOf("=") < eachline.indexOf(":")) {
								separator = "=";
							} else {
								separator = ":";
							}
						} else if (eachline.indexOf("=") > 0) {
							separator = "=";
						} else if (eachline.indexOf(":") > 0) {
							separator = ":";
						}
						int index = eachline.indexOf(separator);
						if (index >= 0) {
							String key = eachline.substring(0, eachline.indexOf(separator));
							String value = eachline.substring(eachline.indexOf(separator) + 1);
							hashParams.put(key, value.trim());
						}
					}
					if (tmpReader != null) {
                        tmpReader.close();
                    }
					if (strReader != null) {
                        strReader.close();
                    }
					bankHeader = DSPCenter.getReqRespHeader(hashParams);
                }else if(!reqSocketReceiver.isHttp || request == null){
                	FGBiz.limitMsg.error("RequestNIO.GetReqRespHeader", "请求内容非xml报文："+clientReqStr);
                }
    		}
    	}
    }

    public Date lastRecDate;

	public Date keyDate;
    
    /**接收字节*/
    public void receiveBytes(){
		try {
			DataInputStream tmpReqInput = null;
			if(reqSocketReceiver.socketClient != null){
				tmpReqInput = new DataInputStream(reqSocketReceiver.socketClient.getInputStream());
			}
			while(!isStopRead()){
				if(lastRecDate == null){
					lastRecDate = new Date();
				}
				if(recByteBuffer == null){
					reqSocketReceiver.reqBytes = new byte[2048];//2K
		    		recByteBuffer = ByteBuffer.wrap(reqSocketReceiver.reqBytes);
		    	}
		    	int tmpReadLen = 0;
		    	if(reqSocketReceiver.clientSocketChannel!=null){
		    		tmpReadLen = reqSocketReceiver.clientSocketChannel.read(recByteBuffer);
		    	}else if(reqSocketReceiver.socketClient != null){
		    		tmpReadLen = tmpReqInput.read(reqSocketReceiver.reqBytes, reqSocketReceiver.receiveOffSet, reqSocketReceiver.reqBytes.length - reqSocketReceiver.receiveOffSet);
		    	}
		    	//if (tmpReadLen < 0) {
		    	//	reqSocketReceiver.isCompleted=true;
				//	recCompleteTime = new Date();
				//	return;
		    	//}
				if (tmpReadLen <= 0) {
					reqSocketReceiver.readZeroCount = reqSocketReceiver.readZeroCount+1;
					if (reqSocketReceiver.contentLength > 0 && reqSocketReceiver.contentStartIndex > 0) {
						double diffSecs = DateHelper.getDifferSecs(new Date(), lastRecDate);
						if(diffSecs > 5){//5秒超时
							reqSocketReceiver.syncCode = "-1";
		                	int expectLen = reqSocketReceiver.contentLength + reqSocketReceiver.contentStartIndex;
		                	int actualLen = reqSocketReceiver.readedLength-reqSocketReceiver.contentStartIndex;
		                	reqSocketReceiver.syncMsg = "接收超时（5秒未收到任何字节），预期总字节数"+expectLen+"，已接收"+reqSocketReceiver.readedLength+"，其中已接收内容字节数"+actualLen+"，预期内容字节数"+reqSocketReceiver.contentLength;
		                	reqSocketReceiver.hasError=true;
		                	reqSocketReceiver.errMsg=reqSocketReceiver.syncMsg;
		                	FGBiz.limitMsg.error("RequestNIO.receiveBytes", reqSocketReceiver.syncMsg);
						}
					}else if(reqSocketReceiver.readedLength > 0 && reqSocketReceiver.readZeroCount >= 15){//之前已接收到字节，现在15次为0
						reqSocketReceiver.isCompleted = true;
						String strAll = new String(reqSocketReceiver.reqBytes, 0, reqSocketReceiver.receiveOffSet, reqSocketReceiver.charsetName);
						FGBiz.limitMsg.error("RequestNIO.receiveBytes", "连续15次接收字节为0，未识别的报文："+strAll);
					}else if(reqSocketReceiver.readZeroCount >= 3){//0字节，接收3次就退出
						reqSocketReceiver.isCompleted = true;
					}
					return;
		        }else{
		        	reqSocketReceiver.readZeroCount = 0;
		        	lastRecDate = new Date();
		        }
				
				reqSocketReceiver.readedLength += tmpReadLen;
				reqSocketReceiver.receiveOffSet += tmpReadLen;
				
				
				reqSocketReceiver.getHeader(bankHeader);
	            
	            if (reqSocketReceiver.contentLength < 0 || reqSocketReceiver.contentStartIndex < 0){
	                if (reqSocketReceiver.isCipherSocket == false && reqSocketReceiver.isSyncFile == false && reqSocketReceiver.receiveOffSet > 200){
	                	reqSocketReceiver.errMsg = "未知的通讯头部：" + new String(reqSocketReceiver.reqBytes, 0, reqSocketReceiver.receiveOffSet, reqSocketReceiver.charsetName);
	                	reqSocketReceiver.contentStartIndex = -1;
	                	reqSocketReceiver.hasError = true;
	                	return;
	                }
	            }
	            
	            if (reqSocketReceiver.contentLength > 0 && reqSocketReceiver.contentStartIndex > 0) {
	                if(reqSocketReceiver.isSyncFile){//文件同步
	                	if(StringHelper.isNullOrEmpty(reqSocketReceiver.oriFilePath)){
	                		reqSocketReceiver.syncCode = "-1";
	                		reqSocketReceiver.syncMsg = "文件传输无文件路径";
	                		reqSocketReceiver.hasError = true;
	                		reqSocketReceiver.errMsg = reqSocketReceiver.syncMsg;
	                        return;
	                	}
	                	if(reqSocketReceiver.saveFile == null){//第一次写入
	                		reqSocketReceiver.openSaveFile(bankHeader);
	                		if((reqSocketReceiver.receiveOffSet-reqSocketReceiver.contentStartIndex) > 0){
	                			reqSocketReceiver.saveFileStream.write(reqSocketReceiver.reqBytes, reqSocketReceiver.contentStartIndex, reqSocketReceiver.receiveOffSet-reqSocketReceiver.contentStartIndex);
	                    		if(reqSocketReceiver.contentLength>102400){//扩大缓冲区
	                    			reqSocketReceiver.reqBytes = new byte[51200];//50K
	                    			recByteBuffer = ByteBuffer.wrap(reqSocketReceiver.reqBytes);
	                    		}else if(reqSocketReceiver.contentLength>40960){//扩大缓冲区
	                    			reqSocketReceiver.reqBytes = new byte[20480];//20K
	                    			recByteBuffer = ByteBuffer.wrap(reqSocketReceiver.reqBytes);
	                    		}else if((reqSocketReceiver.contentLength + reqSocketReceiver.contentStartIndex)>reqSocketReceiver.reqBytes.length){//扩大缓冲区
	                    			reqSocketReceiver.reqBytes = new byte[5120];//5K
	                    			recByteBuffer = ByteBuffer.wrap(reqSocketReceiver.reqBytes);
	                    		}
	                		}
	                	}else{//后续写入
	                		reqSocketReceiver.saveFileStream.write(reqSocketReceiver.reqBytes, 0, reqSocketReceiver.receiveOffSet);
	                	}
	                	reqSocketReceiver.receiveOffSet = 0;//重置字节接收
	                	recByteBuffer.clear();
	                }else{
	                	if (reqSocketReceiver.reqBytes.length < reqSocketReceiver.contentLength + reqSocketReceiver.contentStartIndex) {
	                        byte[] tmpUnionInfo = new byte[reqSocketReceiver.contentLength + reqSocketReceiver.contentStartIndex];//一次性分配内存
	                        recByteBuffer = ByteBuffer.wrap(tmpUnionInfo);
	                        recByteBuffer.put(reqSocketReceiver.reqBytes, 0, reqSocketReceiver.receiveOffSet);
	                        reqSocketReceiver.reqBytes = null;
	                        reqSocketReceiver.reqBytes = tmpUnionInfo;
	                    }
	                }

	                int tmpToReadLen = reqSocketReceiver.contentLength + reqSocketReceiver.contentStartIndex - reqSocketReceiver.readedLength;
	                if (tmpToReadLen == 0) {//完成
	                	reqSocketReceiver.isCompleted = true;
	                	if(reqSocketReceiver.isSyncFile){//文件同步
	                		reqSocketReceiver.syncCode = "0";
	                		reqSocketReceiver.syncMsg = "保存至"+reqSocketReceiver.trgFilePath+"完成";
	                	}
	                	return;
	                }else if(tmpToReadLen < 0){//结束
	                	reqSocketReceiver.syncCode = "-1";
	                	int expectLen = reqSocketReceiver.contentLength + reqSocketReceiver.contentStartIndex;
	                	int actualLen = reqSocketReceiver.readedLength-reqSocketReceiver.contentStartIndex;
	                	reqSocketReceiver.syncMsg = "实际接收总字节数"+reqSocketReceiver.readedLength+"与预期"+expectLen+"不符，其中实际接收内容字节数"+actualLen+"，预期内容字节数"+reqSocketReceiver.contentLength;
	                	reqSocketReceiver.hasError=true;
	                	reqSocketReceiver.errMsg=reqSocketReceiver.syncMsg;
	                	return;
	                }
	            }else if (reqSocketReceiver.contentLength == 0){
	            	reqSocketReceiver.isCompleted = true;
	            	return;
	            }	
			}
		} catch(Throwable e){
			reqSocketReceiver.isNeedCloseConn = true;
			reqSocketReceiver.hasError = true;
			reqSocketReceiver.errMsg = LogHelper.getStackTraceFirstLine(e);
			FGBiz.limitMsg.exception("RequestNIO.receiveBytes", e);
		}
    }
    
    public void writeBytesForRemoting(byte[] tmpBytesResp){
    	try {
    		synchronized(reqSocketReceiver.socketClient){
				DataOutputStream tmpRespOutput = new DataOutputStream(reqSocketReceiver.socketClient.getOutputStream());
				tmpRespOutput.write(tmpBytesResp);
    		}
		} catch (IOException e) {
			FGBiz.limitMsg.exception("RequestNIO.writeBytesForRemoting", e);
		}
    }
    
    public void writeBytes(byte[] pBytes) throws Throwable{
    	try{
    		ByteBuffer writeBuffer = ByteBuffer.wrap(pBytes);
    		reqSocketReceiver.clientSocketChannel.write(writeBuffer);
            
    		//DataOutputStream tmpRespOutput = new DataOutputStream(reqSocketReceiver.socketClient.getOutputStream());
    		//tmpRespOutput.write(pBytes);
    	}catch(Throwable e){
    		reqSocketReceiver.isNeedCloseConn = true;
    		FGBiz.limitMsg.exception("RequestNIO.WriteBytes", e);
    		throw(e);
    	}
    }
    
    /**是否结束读取请求字节*/
    public boolean isStopRead(){
    	if(reqSocketReceiver.isCompleted || reqSocketReceiver.hasError){
    		
    		if(reqSocketReceiver.saveFileStream!=null){
    			try {
					reqSocketReceiver.saveFileStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
    			reqSocketReceiver.saveFileStream = null;
            }
    		
    		return true;
    	}
    	return false;
    }

	/**
	 * 数据解密与验签
	 * @return
     */
	public void dataDecryptAndChk()throws Exception{
		//如果有原文获取原文
		String reqPlain =StringHelper.getTagContext(clientReqStr,"plain");
		//获取日期，用于获取密钥
		String dateStr =StringHelper.getTagContext(clientReqStr,"date");
		//获取租户号
		String tenant = StringHelper.getTagContext(clientReqStr,"tenant");
		if(StringHelper.isNullOrEmpty(tenant)){
			tenant =StringHelper.getTagContext(clientReqStr,"Tenant");
		}
		//当前客户需要加密
		if(FGBiz.dspCenter.dspSet.enableEncrypt&&FGBiz.dspCenter.dspSet.certConfig.custCerMap.get(tenant)!=null){
			if(!reqSocketReceiver.isReqSign){
				throw new Exception(String.format("客户【%s】的请求没有加密和签名",tenant));
			}
		}
		//
		else {
			if(reqSocketReceiver.isReqSign){
				throw new Exception(String.format("目标dsp没有为当前租户开启通信加密功能!",tenant));
			}
		}
		if(reqSocketReceiver.isReqEncrypt){
			//获取密文
			String encryptDataStr = StringHelper.getTagContext(clientReqStr,"encrypt");
			Date date = Format.Date8Format.parse(dateStr);
			//响应报文获取密钥时使用
			keyDate =date;
			SecretKeyDataHelper helper = new SecretKeyDataHelper(tenant,date);
			String aesKey  = helper.getKey(date);
			reqPlain = AESUtil.decrypt(encryptDataStr, aesKey,reqSocketReceiver.charsetName);
			sbLog.append("\r\n"+Format.DateTimeSSSFormat.format(new Date()) + " " + seqIDInner +  " " +
					String.format("使用客户【%s】日期【%s】的密钥【%s】解密成功\r\n",tenant,dateStr,aesKey));
			sbLog.append("\r\n"+Format.DateTimeSSSFormat.format(new Date()) + " " + seqIDInner + " " +"解密之后的原文信息：\r\n"+reqPlain);
		}
		if(reqSocketReceiver.isReqSign){
			String signDataStr = StringHelper.getTagContext(clientReqStr,"signature");
			boolean verify = SignEncryptHelper.verify(reqPlain.getBytes(reqSocketReceiver.charsetName), Base64.decodeBase64(signDataStr),FGBiz.dspCenter.dspSet.certConfig.custCerMap.get(tenant));
			if(verify){
				clientReqStr =reqPlain;
				sbLog.append("\r\n"+Format.DateTimeSSSFormat.format(new Date()) + " " + seqIDInner + " " +String.format("使用客户【%s】的公钥验签成功",tenant)+"\r\n");
			}else {
				sbLog.append("\r\n"+Format.DateTimeSSSFormat.format(new Date()) + " " + seqIDInner + " " +String.format("使用客户【%s】的公钥验签失败",tenant)+"\r\n");
				throw new Exception(String.format("使用客户【%s】的公钥验签失败",tenant));
			}
		}
	}

}
